package com.demo.code;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;

public class StatisticTotalCodeLine {


    public static void main(String[] args) throws IOException {
        StringBuilder executeCmd = new StringBuilder();
        //executeCmd.append("cmd /c cd " + "D:\\data\\test_template ");
        //527e0f2b  八位commitId
//        executeCmd.append(" &&  git clone -b feature-code-cover git@gitee.com:gogogo_cc/tool_managementt.git tool && cd D:\\data\\test_template\\tool && git checkout  feature-01 ");
        // executeCmd.append(" &&  git clone -b feature-code-cover git@gitee.com:gogogo_cc/tool_managementt.git tool && cd D:\\data\\test_template\\tool &&  find . \"(\" -name \"*.java\" -or -name \"*.xml\"  -or -name \"*.yml\"  \")\" -print | xargs wc -l");
//        executeCmd.append("D:\\software\\Git\\Git2.39.2\\Git\\bin\\bash.exe /c cd D:\\data\\test_template  &&  git clone -b feature-code-cover git@gitee.com:gogogo_cc/tool_managementt.git tool && cd D:\\data\\test_template\\tool\\ &&  git ls-files | xargs wc -l");
        executeCmd.append("cmd /c cd D:\\data\\test_template  &&  git clone -b feature-code-cover git@gitee.com:gogogo_cc/tool_managementt.git tool && cd D:\\data\\test_template\\tool\\ &&  git ls-files");
        System.out.println("克隆：" + executeCmd);
        Process result = Runtime.getRuntime().exec(String.valueOf(executeCmd));
        InputStream reportInputStream = result.getInputStream();
        InputStreamReader reportInputStreamReader = new InputStreamReader(reportInputStream, StandardCharsets.UTF_8);
        BufferedReader reportBufferedReader = new BufferedReader(reportInputStreamReader);
        String strInput;
        ArrayList<String> fileNameList = new ArrayList<>();
        while (!StringUtils.isEmpty(strInput = reportBufferedReader.readLine())) {
            //输出是为了获取最新的文件
            System.out.println("clone result output=" + strInput);
            if (strInput.endsWith(".java") || strInput.endsWith(".xml")) {
                fileNameList.add("D:\\data\\test_template\\tool\\" + strInput);
            }
        }
        //FileUtils.forceDelete(new File("D:\\data\\test_template\\tool"));
        int total;
        if (!CollectionUtils.isEmpty(fileNameList)) {
            for (String filePath : fileNameList) {
                StringBuilder command = new StringBuilder();
                command.append("cmd /c wc -l " + filePath);
                Process process = Runtime.getRuntime().exec(String.valueOf(command));
                InputStream inputStream = process.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String strInputResult;
                while (!StringUtils.isEmpty(strInputResult = bufferedReader.readLine())) {
                    //输出是为了获取最新的文件
                    System.out.println("statistic result output=" + strInputResult);
                }
            }
        }
//        String gitRepoPath = "D:\\data\\test_template\\tool\\"; // 替换为你的Git仓库路径
//        try {
//            int codeLineCount = countCodeLines(gitRepoPath);
//            System.out.println("代码总行数: " + codeLineCount);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public static int countCodeLines(String gitRepoPath) throws Exception {
        ProcessBuilder processBuilder = new ProcessBuilder("git", "ls-tree", "-r", "--name-only", gitRepoPath);
        Process process = processBuilder.start();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            int codeLineCount = 0;
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("输出line:" + line);
                if (line.endsWith(".java") || line.endsWith(".py") || line.endsWith(".cpp") || line.endsWith(".c") ||
                        line.endsWith(".js") || line.endsWith(".css") || line.endsWith(".go") || line.endsWith(".ts") ||
                        line.endsWith(".kt") || line.endsWith(".swift") || line.endsWith(".scss")) {
                    codeLineCount += countLinesInFile(gitRepoPath + "/" + line);
                }
            }
            return codeLineCount;
        }
    }

    private static int countLinesInFile(String filePath) throws Exception {
        ProcessBuilder processBuilder = new ProcessBuilder("wc", "-l", filePath);
        Process process = processBuilder.start();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                return Integer.parseInt(line.trim().split(" ")[0]);
            }
        }
        return 0;
    }

}
